//
//  BusinessCardUpdateViewController.m
//  XinKaiFa55like
//
//  Created by junseek on 2017/4/11.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "BusinessCardUpdateViewController.h"

@interface BusinessCardUpdateViewController ()<UITextFieldDelegate>{
    
    UIScrollView *scrollBG;
    
    UITextField *txtName;    
    UITextField *txtZW;
    UITextField *txtAddress;
    
}

@end

@implementation BusinessCardUpdateViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //注册监听键盘事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];    
    
    RemoveNofify
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self rightButton:@"提交" image:nil sel:@selector(rightButtonClicked)];
    
    
    scrollBG=[[UIScrollView alloc] initWithFrame:CGRectMake(0, kTopHeight, kScreenWidth, kContentHeight)];
    scrollBG.backgroundColor=rgbGray;
    [self.view addSubview:scrollBG];
    
    
    NSInteger tempY=10;
    UIView *viewBG=[[UIView alloc] initWithFrame:CGRectMake(0, 10, kScreenWidth, 44*3)];
    viewBG.backgroundColor=[UIColor whiteColor];
    [scrollBG addSubview:viewBG];
    
    [scrollBG addSubview:[RHMethods labelWithFrame:CGRectMake(10, tempY, 0, 44) font:fontTitle color:rgbTitleColor text:@"公司名称"]];
    txtName=[RHMethods textFieldlWithFrame:CGRectMake(100, tempY, kScreenWidth-110, 44) font:fontTitle color:rgbTitleColor placeholder:@"请输入" text:@""];
    [txtName setDelegate:self];
    [scrollBG addSubview:txtName];
    
    tempY+=44;
    [scrollBG addSubview:[RHMethods imageviewWithFrame:CGRectMake(0, tempY-0.5, kScreenWidth, 0.5) defaultimage:@"userLine"]];
    
    [scrollBG addSubview:[RHMethods labelWithFrame:CGRectMake(10, tempY, 0, 44) font:fontTitle color:rgbTitleColor text:@"公司职位"]];
    txtZW=[RHMethods textFieldlWithFrame:CGRectMake(100, tempY, kScreenWidth-110, 44) font:fontTitle color:rgbTitleColor placeholder:@"请输入" text:@""];
    [txtZW setDelegate:self];
    [scrollBG addSubview:txtZW];
    
    tempY+=44;
    [scrollBG addSubview:[RHMethods imageviewWithFrame:CGRectMake(0, tempY-0.5, kScreenWidth, 0.5) defaultimage:@"userLine"]];
    
    [scrollBG addSubview:[RHMethods labelWithFrame:CGRectMake(10, tempY, 0, 44) font:fontTitle color:rgbTitleColor text:@"公司地址"]];
    txtAddress=[RHMethods textFieldlWithFrame:CGRectMake(100, tempY, kScreenWidth-110, 44) font:fontTitle color:rgbTitleColor placeholder:@"请输入" text:@""];
    [txtAddress setDelegate:self];
    [scrollBG addSubview:txtAddress];
    
    tempY+=44;
    [scrollBG addSubview:[RHMethods imageviewWithFrame:CGRectMake(0, tempY-0.5, kScreenWidth, 0.5) defaultimage:@"userLine"]];
    
    [scrollBG setContentSize:CGSizeMake(kScreenWidth, tempY+20)];
    [self loadContentData];
}

-(void)loadContentData{
    [NetEngine createGetAction_LJ_two:[NSString stringWithFormat:XKFV3userinformation,[[Utility Share] userId],[[Utility Share] userToken]] onCompletion:^(id resData, BOOL isCache) {
        if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
            NSDictionary * dicData=[resData objectForJSONKey:@"data"];
            txtName.text=[dicData valueForJSONStrKey:@"companyname"];
            txtZW.text=[dicData valueForJSONStrKey:@"companyposition"];
            txtAddress.text=[dicData valueForJSONStrKey:@"companyaddr"];
        }else{
            [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [super backButtonClicked:nil];
            });
        }
    } onError:^(NSError *error) {
        [SVProgressHUD showImage:nil status:alertErrorTxt];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [super backButtonClicked:nil];
        });
    }];
    
}
#pragma mark selector
-(void)handleKeyboardDidShow:(NSNotification *)notification{
    NSDictionary *info = [notification userInfo];
    CGRect keyboardFrame;
    [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
    CGFloat distanceToMove = kbSize.height;
    DLog(@"---->动态键盘高度:%f",distanceToMove);
    
    [UIView animateWithDuration:0.3 animations:^{
        scrollBG.frameHeight=kContentHeight-distanceToMove;
        
    }];
}
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3 animations:^{
        scrollBG.frameHeight=kContentHeight;
        
        
    }];
}//

#pragma mark button
-(void)rightButtonClicked{
    [self.view endEditing:YES];
    NSString *strName=txtName.text;
    NSString *strZW=txtZW.text;
    NSString *strAddress=txtAddress.text;
    
    
    NSMutableDictionary *dicT=[NSMutableDictionary new];
    [dicT setValue:[[Utility Share] userToken] forKey:@"token"];
    [dicT setValue:[[Utility Share] userId] forKey:@"uid"];
    
    [dicT setValue:strName forKey:@"companyname"];
    [dicT setValue:strZW forKey:@"companyposition"];
    [dicT setValue:strAddress forKey:@"companyaddr"];
    
    [NetEngine createPostAction:XKFuserprofiles withParams:dicT onCompletion:^(id resData, BOOL isCache) {
        if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
            [SVProgressHUD showSuccessWithStatus:[resData valueForJSONStrKey:@"info"]];
            [self backRefreshData];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [super backButtonClicked:nil];
            });
        }else{
            [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
        }
    }];
    

}

-(void)backRefreshData{
    for (id controller in self.navigationController.viewControllers) {
        if ([NSStringFromClass([controller class]) isEqualToString:@"SelectWebUrlViewController"]) {
            if ([(NSObject*)controller respondsToSelector:@selector(popRefreshData)]) {
                [controller performSelector:@selector(popRefreshData) withObject:nil afterDelay:0.01];
            }
            continue;
        }
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
